<template>
  <div>
    <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in employees" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departments.js'
export default {
  name: 'VueAdminTemplateDeptDialog',
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    const validateCode = (rule, value, callback) => {
      let codeList = null
      if (this.isEdit) {
        codeList = this.originList.filter(item => item.id !== this.id).map(({ code }) => code)
      } else {
        codeList = this.originList.map(({ code }) => code)
      }
      codeList.includes(value) ? callback(new Error(value + '已存在')) : callback()
    }
    const validateName = (rule, value, callback) => {
      let nameList = null
      if (this.isEdit) {
        const pid = this.originList.find(item => item.id === this.id).pid
        nameList = this.originList.filter(item => item.pid === pid && item.id !== this.id).map(({ name }) => { name })
      } else {
        nameList = this.originList.filter(item => item.pid === this.id).map(({ name }) => name)
      }
      nameList.includes(value) ? callback(new Error(value + '已存在')) : callback()
    }
    return {
      employees: [],
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 2, max: 10, message: '请输入 2 ~ 10 个字符', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 2, max: 10, message: '请输入 2 ~ 10 个字符', trigger: 'blur' },
          { validator: validateCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请选择部门负责人', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 2, max: 300, message: '请输入 2 ~ 300 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 获取下拉员工数据
    this.loadEmployee()
    // 获取部门详情
    this.loadDetail()
  },

  methods: {
    // 获取下拉员工数据
    async loadEmployee() {
      try {
        const res = await getEmployeeSimple()
        this.employees = res.data
      } catch (error) {
        console.log(error.message)
      }
    },
    // 获取详情数据回填
    async loadDetail() {
      if (!this.isEdit) return
      const res = await getDepartDetail(this.id)
      this.form = res.data
    },
    async  hSubmit() {
      this.$refs.deptForm.validate(async valid => {
        try {
          if (!valid) return
          let res = null
          if (this.isEdit) {
            res = await updateDepartments(this.form)
          } else {
            this.form.pid = this.id
            res = await addDepartments(this.form)
          }
          this.$message.success(res.message)
          this.$emit('success')
        } catch (error) {
          this.$message.error(error.message)
        }
      })
    },
    hCancel() {
      this.$emit('close')
    },
    clearForm() {
      this.$refs.deptForm.resetFields()
    }

  }
}
</script>

<style lang="scss" scoped>

</style>
